package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSevice;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/9 10:12
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleSevice roleSevice;

    @PostMapping("/getByConditions")
    public Result getByConditions(@RequestBody QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        try {
            PageResult pageResult = roleSevice.getByConditions(queryPageBean);
            return new Result(true,"角色查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"角色查询失败");
        }
    }

    /**
     * 获取全部的菜单和权限信息
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll() {

        try {
            Map<String, Object> map= roleSevice.getAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
        try {
            roleSevice.add(role, menuIds,permissionIds);
            return new Result(true,"角色添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色添加失败");
        }
    }

    /**
     * 用于修改时回显数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public Result get(Integer id) {
        Role role = null;
        try {
            role = roleSevice.get(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 查询所有关联的中间表数据
     * @param id
     * @return
     */
    @GetMapping("/getMenuIdsAndPermissionIdsById")
    public Result getMenuIdsAndPermissionIdsById(Integer id) {

        try {
            Map<String,List<Integer>> map=roleSevice.getMenuIdsAndPermissionIdsById(id);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 更新角色及中间表
     * @param role
     * @param menuIds
     * @param permissionIds
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
        //调用service传入参数
        try {
            roleSevice.update(role, menuIds,permissionIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组，并且删除中间表信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        try {
            roleSevice.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }



}
